package cz.kfad.ofrss.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import cz.kfad.ofrss.R;

import java.util.List;

public abstract class ListAdapter<T> extends ArrayAdapter<T> {

    public ListAdapter(Context context, List<T> objects) {
        super(context, R.layout.list_item, objects);
    }


    private static class ViewHolder {
        public TextView textView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView;
        if (convertView != null) {
            itemView = convertView;
        } else {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.list_item, null, true);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) itemView.findViewById(R.id.list_item_text);
            itemView.setTag(viewHolder);
        }

        ViewHolder viewHolder = (ViewHolder) itemView.getTag();
        viewHolder.textView.setText(getLabel(getItem(position)));
        return itemView;
    }

    public abstract CharSequence getLabel(T item);
}
